﻿Partial Public Class frmChangePassword
    Dim dbUsersTable As ATSDataClassesDataContext = New ATSDataClassesDataContext
    Dim numberOfTries As Integer
    Dim passwordRetries As Integer

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        DialogResult = False
        Me.Close()
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        
        If txtOldPassword.Password.Length > 0 And txtNewPassword.Password.Length > 0 And txtConfirmPassword.Password.Length > 0 Then
            If txtNewPassword.Password = txtConfirmPassword.Password Then
                Dim oldPassword = From oldPass In dbUsersTable.UserTables _
                                  Where oldPass.Username = RibbonMain.v_username
                With oldPassword.First
                    If GencPros_Authenticate.EncryptDecrypt.VerifyHash(txtOldPassword.Password, "SHA512", .Hashed, Convert.FromBase64String(.Salt)) Then
                        Dim saltUsed As Byte() = GencPros_Authenticate.EncryptDecrypt.ComputeSalt

                        .Hashed = GencPros_Authenticate.EncryptDecrypt.ComputeHash(txtNewPassword.Password, "SHA512", saltUsed)
                        .AccountLocked = False
                        .Salt = Convert.ToBase64String(saltUsed)
                        dbUsersTable.SubmitChanges()
                        Me.Close()
                    Else
                        MessageBox.Show("Old password does not match.")
                    End If
                End With

            Else
                MessageBox.Show("New password does not match.")
            End If
        Else
            MessageBox.Show("All fields are required.")
        End If

    End Sub

End Class